package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.oss.OssProperties;
import com.ruoyi.common.config.oss.OssTemplate;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDangerKeep;
import com.ruoyi.system.mapper.SysDangerKeepMapper;
import com.ruoyi.system.service.ISysDangerKeepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDangerKeepServiceImpl implements ISysDangerKeepService {

    @Autowired
    private OssTemplate template;

    @Autowired
    private OssProperties ossProperties;

    @Autowired
    private SysDangerKeepMapper sysDangerKeepMapper;

    @Override
    public void create(SysDangerKeep sysDangerKeep) {
        sysDangerKeepMapper.create(sysDangerKeep);
    }

    @Override
    public List<SysDangerKeep> list() {
        return sysDangerKeepMapper.list();
    }

    @Override
    public void delete(Integer id) {
        SysDangerKeep sysDangerKeep = sysDangerKeepMapper.getById(id);
        String fileName = sysDangerKeep.getFileName();
        if (StringUtils.isNotNull(fileName)) {
            try {
                template.removeObject(ossProperties.getBucketName(),fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sysDangerKeepMapper.delete(id);
    }

    @Override
    public void update(SysDangerKeep sysDangerKeep) {
        sysDangerKeepMapper.update(sysDangerKeep);
    }

    @Override
    public void deleteFile(Integer id) {
        sysDangerKeepMapper.updateFileColumn(id);
    }
}
